package com.gitee.zhziyun.cnfsystem.works;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.gitee.zhziyun.cnfsystem.config.IpPortConfig;
import com.gitee.zhziyun.cnfsystem.master.AppMaster;
import com.gitee.zhziyun.cnfsystem.model.Command;
import com.gitee.zhziyun.cnfsystem.model.FileAccept;
import com.gitee.zhziyun.cnfsystem.model.FileSource;
import com.gitee.zhziyun.cnfsystem.utils.JSONUtil;
import com.gitee.zhziyun.cnfsystem.utils.udputil.MulticastReceiver;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

public class MulticastWorker implements Worker {
    private final AppMaster appMaster;
    private final Queue<String> multicastQueue;
    private final MulticastReceiver multicastReceiver;
    private final MulticastMessageHandler multicastHandler;

    public MulticastWorker(AppMaster appMaster) {
        this.appMaster = appMaster;
        this.multicastQueue = new ConcurrentLinkedQueue<>();
        this.multicastReceiver = new MulticastReceiver(multicastQueue);
        this.multicastHandler = new MulticastMessageHandler();
    }

    @Override
    public void start() {
        multicastHandler.start();
        multicastReceiver.start();
    }

    @Override
    public void stop() {
        multicastReceiver.close();
        multicastHandler.close();
    }

    // 处理组播消息
    private class MulticastMessageHandler extends Thread {
        private boolean running = true;
        @Override
        public void run() {
            while (running) {
                String multicastMessage = multicastQueue.poll();
                if (multicastMessage != null) {
                    String[] parts = multicastMessage.split(":", 2);
                    if (parts.length < 2) continue;  // 防止格式错误

                    try {
                        String senderIp = parts[0];
                        Command command = JSONUtil.stringToObject(parts[1], Command.class);
                        String option = command.getOption();

                        // 处理消息
                        if ("multicastMessage".equals(option)) {
                            System.out.println("收到组播消息: " + senderIp + ":" + command.getData());
                            appMaster.fireMulticastMessage(senderIp, command.getData());
                        } else if ("multicastFile".equals(option)) {
                            if (senderIp.equals(IpPortConfig.getLocalIp())) {
                                continue;
                            }
                            System.out.println("收到组播文件消息: " + senderIp);
                            appMaster.fireMulticastFileMessage(senderIp, JSONUtil.stringToObject(command.getData(), FileSource.class));
                        } else if ("multicastAcceptFile".equals(option)) {
                            if (senderIp.equals(IpPortConfig.getLocalIp())) {
                                continue;
                            }
                            System.out.println("收到组播文件接收确认: " + senderIp);
                            appMaster.fireMulticastFileAccept(senderIp, JSONUtil.stringToObject(command.getData(), FileAccept.class));
                        }
                    } catch (JsonProcessingException e) {
                        System.err.println("JSON解析错误: " + e.getMessage());
                    } catch (Exception e) {
                        System.err.println("组播处理类出现错误: " + e.getMessage());
                    }
                }
            }
        }

        public void close() {
            running = false;
        }
    }
}
